<template>
  <div class="search">
    <!-- trigger-on-focus 这个默认是true，设置为false之后，不会在输入框获取焦点的时候立马发送请求，而是输入完成之后再发---相当于节流  -->
    <el-autocomplete clearable placeholder="请你输入医院名称" :fetch-suggestions="querySearch" v-model="hosname"
      @select="goDetail" :trigger-on-focus='false' />
    <el-button type="primary" :icon="Search"></el-button>
  </div>
</template>

<script lang="ts" setup>
import { Search } from "@element-plus/icons-vue";
import { ref } from 'vue'
import { reqHospitalInfo } from '@/api/home/index'
import type { HospitalInfo } from '@/api/home/type'

import { useRouter } from "vue-router";
//创建路由器对象
let $router = useRouter();

let hosname = ref<string>('')

const querySearch = async (queryString: string, cb: any) => {
  // console.log(queryString, cb);

  let result: HospitalInfo = await reqHospitalInfo(queryString);
  // console.log(result);

  let hospitalArr = result.data.map(item => {//该插件的数据格式要求
    return {
      value: item.hosname,
      hoscode: item.hoscode
    }
  })

  cb(hospitalArr)
}

const goDetail = (item: any) => {
  $router.push({
    path: '/hospital/register', query: {
      hoscode: item.hoscode
    }
  })
}
</script>


<style scoped lang="scss">
.search {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0px;

  :deep(.el-autocomplete) {
    width: 600px;
    margin-right: 10px;
  }
}
</style>